{% extends 'base.html' %}
{% load static %}
{% load i18n %}

{% block custom_head_css_js %}
    <link href='{% static "css/plugins/select2/select2.min.css" %}' rel="stylesheet">
    <link href='{% static "css/plugins/sweetalert/sweetalert.css" %}' rel="stylesheet">
    <script src='{% static "js/plugins/select2/select2.full.min.js" %}'></script>
    <script src='{% static "js/plugins/sweetalert/sweetalert.min.js" %}'></script>
{% endblock %}

{% block content %}
    <div class="wrapper wrapper-content animated fadeInRight">
        <div class="row">
            <div class="col-sm-12">
                <div class="ibox float-e-margins">
                    <div class="panel-options">
                        <ul class="nav nav-tabs">
                            <li class="active">
                                <a href="{% url 'assets:asset-detail' pk=asset.id %}" class="text-center"><i class="fa fa-laptop"></i> {% trans 'Asset detail' %} </a>
                            </li>
                            {% if user.is_superuser %}
                            <li class="pull-right">
                                <a class="btn btn-outline btn-default" href="{% url 'assets:asset-update' pk=asset.id %}"><i class="fa fa-edit"></i>{% trans 'Update' %}</a>
                            </li>
                            <li class="pull-right">
                                <a class="btn btn-outline btn-danger btn-delete-asset">
                                    <i class="fa fa-trash-o"></i>{% trans 'Delete' %}
                                </a>
                            </li>
                            {% endif %}
                        </ul>
                    </div>
                    <div class="tab-content">
                        <div class="col-sm-7" style="padding-left: 0">
                            <div class="ibox float-e-margins">
                                <div class="ibox-title">
                                    <span class="label"><b>{{ asset.hostname }}</b></span>
                                    <div class="ibox-tools">
                                        <a class="collapse-link">
                                            <i class="fa fa-chevron-up"></i>
                                        </a>
                                        <a class="dropdown-toggle" data-toggle="dropdown" href="#">
                                            <i class="fa fa-wrench"></i>
                                        </a>
                                        <ul class="dropdown-menu dropdown-user">
                                        </ul>
                                        <a class="close-link">
                                            <i class="fa fa-times"></i>
                                        </a>
                                    </div>
                                </div>
                                <div class="ibox-content">
                                    <table class="table">
                                        <tbody>
                                        <tr class="no-borders-tr">
                                            <td width="20%">{% trans 'Hostname' %}:</td>
                                            <td><b>{{ asset.hostname }}</b></td>
                                        </tr>
                                        <tr>
                                            <td>{% trans 'IP' %}:</td>
                                            <td><b>{{ asset.ip }}</b></td>
                                        </tr>
                                        <tr>
                                            <td>{% trans 'Public IP' %}:</td>
                                            <td><b>{{ asset.public_ip|default:"" }}</b></td>
                                        </tr>
                                        <tr>
                                            <td>{% trans 'Port' %}:</td>
                                            <td><b>{{ asset.port }}</b></td>
                                        </tr>
                                        <tr>
                                            <td>{% trans 'Protocol' %}:</td>
                                            <td><b>{{ asset.protocol }}</b></td>
                                        </tr>
                                        <tr>
                                            <td>{% trans 'Admin user' %}:</td>
                                            <td><b>{{ asset.admin_user }}</b></td>
                                        </tr>
                                        <tr>
                                            <td>{% trans 'Domain' %}:</td>
                                            <td><b>{{ asset.domain|default:"" }}</b></td>
                                        </tr>
                                        <tr>
                                            <td>{% trans 'Vendor' %}:</td>
                                            <td><b>{{ asset.vendor|default:"" }}</b></td>
                                        </tr>
                                        <tr>
                                            <td>{% trans 'Model' %}:</td>
                                            <td><b>{{ asset.model|default:"" }}</b></td>
                                        </tr>
                                        <tr>
                                            <td>{% trans 'CPU' %}:</td>
                                            <td><b>{{ asset.cpu_model|default:"" }} {{ asset.cpu_count|default:"" }}*{{ asset.cpu_cores|default:"" }}</b></td>
                                        </tr>
                                        <tr>
                                            <td>{% trans 'Memory' %}:</td>
                                            <td><b>{{ asset.memory|default:"" }}</b></td>
                                        </tr>
                                        <tr>
                                            <td>{% trans 'Disk' %}:</td>
                                            <td><b>{{ asset.disk_total|default:"" }}</b></td>
                                        </tr>
                                        <tr>
                                            <td>{% trans 'Platform' %}:</td>
                                            <td><b>{{ asset.platform|default:"" }}</b></td>
                                        </tr>
                                        <tr>
                                            <td>{% trans 'OS' %}:</td>
                                            <td><b>{{ asset.os|default:"" }} {{ asset.os_version|default:"" }} {{ asset.os_arch|default:"" }}</b></td>
                                        </tr>
                                        <tr>
                                            <td>{% trans 'Is active' %}:</td>
                                            <td><b>{{ asset.is_active|yesno:"Yes,No" }}</b></td>
                                        </tr>
                                        <tr>
                                            <td>{% trans 'Serial number' %}:</td>
                                            <td><b>{{ asset.sn|default:"" }}</b></td>
                                        </tr>
                                        <tr>
                                            <td>{% trans 'Asset number' %}:</td>
                                            <td><b>{{ asset.number|default:"" }}</b></td>
                                        </tr>
                                        <tr>
                                            <td>{% trans 'Created by' %}:</td>
                                            <td><b>{{ asset.created_by }}</b></td>
                                        </tr>
                                        <tr>
                                            <td>{% trans 'Date joined' %}:</td>
                                            <td><b>{{ asset.date_joined|date:"Y-m-j H:i:s" }}</b></td>
                                        </tr>
                                        <tr>
                                            <td>{% trans 'Comment' %}:</td>
                                            <td><b>{{ asset.comment }}</b></td>
                                        </tr>
                                        </tbody>
                                    </table>
                                </div>
                            </div>
                        </div>
                        {% if user.is_superuser or user.is_org_admin %}
                        <div class="col-sm-5" style="padding-left: 0;padding-right: 0">
                            <div class="panel panel-primary">
                                <div class="panel-heading">
                                    <i class="fa fa-info-circle"></i> {% trans 'Quick modify' %}
                                </div>
                                <div class="panel-body">
                                    <table class="table">
                                        <tbody>
                                        <tr class="no-borders-tr">
                                          <td width="50%">{% trans 'Active' %}:</td>
                                            <td>
                                              <span class="pull-right">
                                                  <div class="switch">
                                                      <div class="onoffswitch">
                                                          <input type="checkbox" {% if asset.is_active %} checked {% endif %}  class="onoffswitch-checkbox" id="is_active">
                                                          <label class="onoffswitch-label" for="is_active">
                                                              <span class="onoffswitch-inner"></span>
                                                              <span class="onoffswitch-switch"></span>
                                                          </label>
                                                      </div>
                                                  </div>
                                              </span>
                                          </td>
                                        </tr>
                                        {% if asset.protocol == 'ssh' %}
                                        <tr>
                                            <td>{% trans 'Refresh hardware' %}:</td>
                                            <td>
                                                <span class="pull-right">
                                                    <button type="button" class="btn btn-primary btn-xs" id="btn_refresh_asset" style="width: 54px">{% trans 'Refresh' %}</button>
                                                </span>
                                            </td>
                                        </tr>
                                        <tr>
                                            <td>{% trans 'Test connective' %}:</td>
                                            <td>
                                                <span class="pull-right">
                                                    <button type="button" class="btn btn-primary btn-xs" id="btn-test-is-alive" style="width: 54px">{% trans 'Test' %}</button>
                                                </span>
                                            </td>
                                        </tr>
                                        {% endif %}
                                        </tbody>
                                    </table>
                                </div>
                            </div>

                            <div class="panel panel-info">
                                <div class="panel-heading">
                                    <i class="fa fa-info-circle"></i> {% trans 'Nodes' %}
                                </div>
                                <div class="panel-body">
                                    <table class="table group_edit" id="add-asset2group">
                                        <tbody>
                                        <form>
                                            <tr>
                                                <td colspan="2" class="no-borders">
                                                    <select data-placeholder="{% trans 'Nodes' %}" id="groups_selected" class="select2 groups" style="width: 100%" multiple="" tabindex="4">
                                                        {% for node in nodes_remain %}
                                                        <option value="{{ node.id }}" id="opt_{{ node.id }}" >{{ node }}</option>
                                                        {% endfor %}
                                                    </select>
                                                </td>
                                            </tr>
                                            <tr>
                                                <td colspan="2" class="no-borders">
                                                    <button type="button" class="btn btn-info btn-sm" id="btn-update-nodes">{% trans 'Confirm' %}</button>
                                                </td>
                                            </tr>
                                        </form>

                                        {% for node in asset.nodes.all %}
                                        <tr>
                                          <td ><b class="bdg_node" data-gid={{ node.id }}>{{ node }}</b></td>
                                          <td>
                                              <button class="btn btn-danger pull-right btn-xs btn-leave-node" type="button"><i class="fa fa-minus"></i></button>
                                          </td>
                                        </tr>
                                        {% endfor %}
                                        </tbody>
                                    </table>
                                </div>
                            </div>

                            <div class="panel panel-warning">
                                <div class="panel-heading">
                                    <i class="fa fa-info-circle"></i> {% trans 'Labels' %}
                                </div>
                                <div class="panel-body">
                                    <ul class="tag-list" style="padding: 0">
                                        {% for label in asset.labels.all %}
                                            <li ><a href=""><i class="fa fa-tag"></i> {{ label.name }}:{{ label.value }}</a></li>
                                        {% endfor %}
                                    </ul>
                                </div>
                            </div>
                        {% endif %}
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
{% endblock %}
{% block custom_foot_js %}
<script>
jumpserver.nodes_selected = {};
function updateAssetNodes(nodes) {
    var the_url = "{% url 'api-assets:asset-detail' pk=asset.id %}";
    var body = {
        nodes: Object.assign([], nodes)
    };
    var success = function(data) {
        // remove all the selected groups from select > option and rendered ul element;
        $('.select2-selection__rendered').empty();
        $('#groups_selected').val('');
        $.map(jumpserver.nodes_selected, function(group_name, index) {
            $('#opt_' + index).remove();
            // change tr html of user groups.
            $('#add-asset2group tbody').append(
                '<tr>' +
                '<td><b class="bdg_node" data-gid="' + index + '">' + group_name + '</b></td>' +
                '<td><button class="btn btn-danger btn-xs pull-right btn-leave-node" type="button"><i class="fa fa-minus"></i></button></td>' +
                '</tr>'
            )
        });
        // clear jumpserver.groups_selected
        jumpserver.nodes_selected = {};
    };
    APIUpdateAttr({
        url: the_url,
        body: JSON.stringify(body),
        success: success
    });
}

function refreshAssetHardware() {
    var the_url = "{% url 'api-assets:asset-refresh' pk=asset.id %}";
    var success = function(data) {
        console.log(data);
        var task_id = data.task;
        var url = '{% url "ops:celery-task-log" pk=DEFAULT_PK %}'.replace("{{ DEFAULT_PK }}", task_id);
        window.open(url, '', 'width=800,height=600')
    };
    APIUpdateAttr({
        url: the_url,
        success: success,
        method: 'GET'
    });
}


$(document).ready(function () {
    $('.select2.groups').select2().on('select2:select', function(evt) {
        var data = evt.params.data;
        jumpserver.nodes_selected[data.id] = data.text;
    }).on('select2:unselect', function(evt) {
        var data = evt.params.data;
        delete jumpserver.nodes_selected[data.id]
    });
}).on('click', '#is_active', function () {
    var the_url = '{% url "api-assets:asset-detail" pk=asset.id %}';
    var checked = $(this).prop('checked');
    var body = {
        'is_active': checked
    };
    var success = '{% trans "Update successfully!" %}';
    var status = $(".ibox-content > table > tbody > tr:nth-child(13) > td:last >b").text();
    APIUpdateAttr({
        url: the_url,
        body: JSON.stringify(body),
        success_message: success
    });
    if (status === "False") {
        $(".ibox-content > table > tbody > tr:nth-child(13) > td:last >b").html('True');
    }else{
        $(".ibox-content > table > tbody > tr:nth-child(13) > td:last >b").html('False');
    }
}).on('click', '#btn-update-nodes', function () {
    if (Object.keys(jumpserver.nodes_selected).length === 0) {
       return false;
    }
    var nodes = $('.bdg_node').map(function() {
        return $(this).data('gid');
    }).get();
    $.map(jumpserver.nodes_selected, function(value, index) {
        nodes.push(index);
        $('#opt_' + index).remove();
    });
    updateAssetNodes(nodes)
}).on('click', '.btn-leave-node', function() {
    var $this = $(this);
    var $tr = $this.closest('tr');
    var $badge = $tr.find('.bdg_node');
    var gid = $badge.data('gid');
    var group_name = $badge.html() || $badge.text();
    $('#groups_selected').append(
        '<option value="' + gid + '" id="opt_' + gid + '">' + group_name + '</option>'
    );
    $tr.remove();
    var groups = $('.bdg_node').map(function () {
        return $(this).data('gid');
    }).get();
    updateAssetNodes(groups)
}).on('click', '.btn-delete-asset', function () {
    var $this = $(this);
    var name = "{{ asset.hostname }}";
    var uid = "{{ asset.id }}";
    var the_url = '{% url "api-assets:asset-detail" pk=DEFAULT_PK %}'.replace('{{ DEFAULT_PK }}', uid);
    var redirect_url = "{% url 'assets:asset-list' %}";
    objectDelete($this, name, the_url, redirect_url);
}).on('click', '#btn_refresh_asset', function () {
    refreshAssetHardware()
}).on('click', '#btn-test-is-alive', function () {
    var the_url = "{% url 'api-assets:asset-alive-test' pk=asset.id %}";

    var success = function(data) {
        var task_id = data.task;
        var url = '{% url "ops:celery-task-log" pk=DEFAULT_PK %}'.replace("{{ DEFAULT_PK }}", task_id);
        window.open(url, '', 'width=800,height=600')
    };

    APIUpdateAttr({
        url: the_url,
        method: 'GET',
        success: success
    });
})

</script>
{% endblock %}
